var o = require("WeChatFileSystemManager"),
    i = "NewGameBox";
cc.Class({
    extends: cc.Component,
    properties: {
        itemBox: cc.Node,
        gamePrefab: cc.Prefab,
        gamePrefabScript: "",
        canvas: cc.Node,
        layer: cc.Node,
        version: cc.Label,
        btn: cc.Node
    },
    init: function (t) {
        this.miniGameList = {}, this.miniGameList.wxd5a257d6ee2b8f91 = !0, this.miniGameList.wx515f44394eab5985 = !0,
            this.miniGameList.wxea245f85c9673414 = !0, this.miniGameList.wx08beeb18dc512c2f = !0,
            this.miniGameList.wxf49b0a26d9405058 = !0, this.miniGameList.wxc4e628aa7caa2c07 = !0,
            this.miniGameList.wx0af703a36035c60c = !0, this.miniGameList.wxd9589cd7117d0873 = !0,
            this.miniGameList.wx6616ae605010e605 = !0, this.miniGameList.wx528f5a9cd16be88a = !0;
        var e = cc.sys.localStorage.getItem(i);
        null != e && 0 != e.length || cc.sys.localStorage.setItem(i, JSON.stringify({})),
            o.init("NewGameBox", t), this.startP = cc.v2(.75 * -this.canvas.width, 0), this.endP = cc.v2(.75 * this.canvas.width, 0),
            this.state = "close", this.btn = this.btn.getComponent("exported-Button"), this.btn.init(this),
            this.initItemBox(), this.initJson(t);
    },
    initItemBox: function () {
        var t = cc.winSize;
        t.width / t.height > 2.06 && (console.log("x"), this.itemBox.getComponent("cc.Widget").left = 120);
    },
    initJson: function (t) {
        var e = this,
            n = "https://www.7cgames.cn/transfer.php?url=https://www.7cgames.cn/GameRes/7CGamesBoxWX/NewGameBox/7cgamesBoxData.json?" + t;
        cc.loader.load(n, function (t, n) {
            t ? console.log(t) : e.addList(n);
        });
    },
    addList: function (t) {
        this.serverString = t, this.loadString = cc.sys.localStorage.getItem(i), this.newlist = {},
            this.initList(), this.UpdateLocal();
    },
    UpdateLocal: function () {
        var t = this;
        console.log("整体更新本地");
        var e = JSON.parse(this.serverString),
            n = JSON.parse(cc.sys.localStorage.getItem(i)),
            a = Object.keys(n);
        console.log("本地个数：", a.length), console.log("服务器个数：", e.data.length), a.forEach(function (e) {
            t.newlist[e] ? (t.newlist[e].logo != n[e].logo && o.removeFile({
                url: n[e].logo,
                success: function () {
                    console.log("删除成功");
                }
            }), t.newlist[e].qrcode != n[e].qrcode && o.removeFile({
                url: n[e].qrcode,
                success: function () {
                    console.log("删除成功");
                }
            })) : (console.log("不存在"), console.log("删除旧的" + n[e].title + "本地文件"), o.removeFile({
                url: n[e].logo,
                success: function () {
                    console.log("删除成功");
                }
            }), o.removeFile({
                url: n[e].qrcode,
                success: function () {
                    console.log("删除成功");
                }
            }));
        }), console.log("整体更新完成"), cc.sys.localStorage.setItem(i, JSON.stringify(this.newlist));
    },
    initList: function () {
        var t = this,
            e = this,
            n = JSON.parse(this.serverString).data,
            i = !1;
        wx.getSystemInfo({
            success: function (t) {
                var e = t.platform;
                "ios" == e ? (console.log("ios"), i = !0) : "android" == e ? console.log("android") : "devtools" == e ? (console.log("devtools"),
                    i = !0) : "web" == e && (console.log("web"), i = !0);
            }
        });
        var a = n.length,
            s = this.itemBox.parent.width;
        i && (a = 9, s = (s - 50) / 3);
        for (var c = function (a) {
                var c = n[a];
                if (i && !t.miniGameList[c.appid]) return "continue";
                var r = cc.instantiate(e.gamePrefab);
                r.getComponent(e.gamePrefabScript).init({
                    width: i ? s : null,
                    name: c.title,
                    appid: c.appid,
                    spriteFrame: null,
                    style: c.style,
                    qrcode: c.qrcode,
                    btnCallback: e.btnCallback.bind(e)
                }), r.parent = e.itemBox, o.getFile({
                    url: c.logo,
                    success: function (t) {
                        cc.loader.load(t, function (t, n) {
                            var o = new cc.SpriteFrame(n);
                            r.getComponent(e.gamePrefabScript).setSpriteFrame(o);
                        });
                    },
                    fail: function () {
                        console.log("加载图片失败");
                    }
                }), t.newlist[c.appid] = {
                    desc: c.desc,
                    title: c.title,
                    logo: c.logo,
                    qrcode: c.qrcode
                };
            }, r = 0; r < a; r++) c(r);
    },
    btnCallback: function (t) {
        console.log(t);
        var e = t.appid,
            n = t.qrcode,
            o = this;
        wx.navigateToMiniProgram ? wx.navigateToMiniProgram({
            appId: e,
            path: "",
            success: function () {
                console.log("打开成功");
            },
            fail: function (t) {
                console.log(t), -1 == t.errMsg.indexOf("fail cancel") && o.showQR({
                    qrcode: n
                });
            }
        }) : (console.log("navigateToMiniProgram 低版本适配"), o.showQR({
            qrcode: n
        }));
    },
    showQR: function (t) {
        o.getFile({
            url: t.qrcode,
            success: function (t) {
                wx.previewImage({
                    urls: [t],
                    success: function () {
                        console.log("wei xin previewImage success...");
                    }
                });
            },
            fail: function () {
                console.log("加载图片失败");
            }
        });
    },
    show: function (t) {
        var e = this,
            n = cc.callFunc(function () {
                e.state = "move";
            }, this),
            i = cc.moveBy(.3, e.endP),
            a = cc.callFunc(function () {
                e.state = "open", t.callback && t.callback(), t.appid && (e.newlist[t.appid] ? (this.layer.getComponent("exported-Layer").init({
                    appid: t.appid,
                    title: e.newlist[t.appid].title,
                    desc: e.newlist[t.appid].desc,
                    qrcode: e.newlist[t.appid].qrcode,
                    callback: e.btnCallback.bind(e)
                }), o.getFile({
                    url: e.newlist[t.appid].logo,
                    success: function (t) {
                        cc.loader.load(t, function (t, n) {
                            var o = new cc.SpriteFrame(n);
                            e.layer.getComponent("exported-Layer").setGameLogo(o);
                        });
                    },
                    fail: function () {
                        console.log("加载图片失败");
                    }
                }), this.layer.active = !0) : console.log("不存在"));
            }, this),
            s = cc.sequence(n, i, a);
        "close" == this.state && this.canvas.runAction(s);
    },
    hide: function (t) {
        var e = cc.callFunc(function () {
                this.state = "move";
            }, this),
            n = cc.moveBy(.3, this.startP),
            o = cc.callFunc(function () {
                this.state = "close", t.callback && t.callback();
            }, this),
            i = cc.sequence(e, n, o);
        "open" == this.state && this.canvas.runAction(i);
    },
    readBtn: function () {
        o.showFile();
    },
    clearLocsl: function () {
        cc.sys.localStorage.setItem(i, JSON.stringify({}));
    },
    removeAllFile: function () {
        var t = cc.sys.localStorage.getItem(i),
            e = JSON.parse(t),
            n = Object.keys(e);
        console.log(n), n.forEach(function (t) {
            o.removeFile({
                url: e[t].logo,
                success: function () {
                    console.log("删除成功");
                }
            }), o.removeFile({
                url: e[t].qrcode,
                success: function () {
                    console.log("删除成功");
                }
            });
        }), cc.sys.localStorage.setItem(i, JSON.stringify({}));
    }
})